import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)


// 配置项目路由
const router = new VueRouter({
    mode: "history",
    routes: [{
        path: '/',
        redirect: '/home' // 重定向
    },
        {
            path: '/home', //应用主页面
            name: 'home',
            component: () => import('@/views/home/Home'),
            meta: {
                isShowTabbar: true
            }
        },
        {
            path: '/meet', //会议主页
            name: 'meet',
            component: () => import('@/views/meet/Meet'),
            meta: {
                isShowTabbar: true
            }
        },
        {
            path: '/meet/meetMain', //会议主页
            name: 'meetMain',
            component: () => import('@/views/meet/MeetMain'),
            meta: {
                isShowTabbar: true
            }
        },
        {
            path: '/meet/meetReserve', //我的预定
            name: 'meetReserve',
            component: () => import('@/views/meet/MeetReserve'),
            meta: {
                isShowTabbar: true
            }
        },
        {
            path: '/meet/meetHistory', //我的预定
            name: 'meetHistory',
            component: () => import('@/views/meet/MeetHistory'),
            meta: {
                isShowTabbar: true
            }
        },
        {
            path: '/meet/meetDetail', //我的预定
            name: 'meetDetail',
            component: () => import('@/views/meet/MeetDetail'),
            meta: {
                isShowTabbar: true
            }
        },


        //------------------------------企业微信测试路由-----------------------

        {
            path: '/auth', //首页
            name: 'auth',
            component: () => import('@/views/Auth'),
            meta: {
                isShowTabbar: true
            }
        },
        {
            path: '/callback',
            name: 'callback',
            component: () => import('@/views/AuthCallback'),
            meta: {
                isShowTabbar: true
            }

            // children: [ // 二级路由 不能加'/' 加'/'表示一级路由
            //   {
            //     path: 'searchPopup',
            //     component: () => import('@/views/home/search/searchPopup.vue'),
            //     name: 'searchpopup',
            //     meta: {
            //       isshowtabbar: false
            //     },
            //   }
            // ],

        },
        {
            path: '/user', //我的
            name: 'user',
            component: () => import('@/views/User'),
            meta: {
                isShowTabbar: true
            }
        },
        {
            path: '/complaint', //我的
            name: 'complaint',
            component: () => import('@/views/complaint/Complaint'),
            meta: {
                isShowTabbar: true
            }
        },

        {
            path: '/mapPosition', //我的
            name: 'mapPosition',
            component: () => import('@/views/map/MapPosition'),
            meta: {
                isShowTabbar: true
            }
        },
        {
            path: '/home2', //我的
            name: 'home2',
            component: () => import('@/views/home/Home2'),
            meta: {
                isShowTabbar: false
            }
        },
    ]
})

// 路由前置守卫
router.beforeEach((to, from, next) => {
    // 有token就表示已经登录
    // 想要进入购物车页面，必须有登录标识token
    let token = localStorage.getItem('token')
    if (to.path == '/cart') {
        // 此时必须要有token
        if (token) {
            next(); // next()去到to所对应的路由界面
        } else {
            Vue.prototype.$toast('请先登录');
            // 定时器
            setTimeout(() => {
                next("/user"); // 强制去到"/user"所对应的路由界面
            }, 1000);
        }
    } else {
        // 如果不是去往购物车的路由，则直接通过守卫，去到to所对应的路由界面
        next()
    }
})

// 该段代码不需要记，理解即可
// 在vue-router在3.1.0版本之后，push和replace方法会返回一个promise对象，如果跳转到相同的路由，就报promise uncaught异常
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};

export default router
